﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Configuration;
using System.Net;

namespace Presentation
{
    public static class EmailSender
    {
        static string from = ConfigurationManager.AppSettings["emailSender"];
        static string password = ConfigurationManager.AppSettings["emailPassword"];
        static string smtp = ConfigurationManager.AppSettings["emailSMTP"];
        static string to = ConfigurationManager.AppSettings["emailTo"];
        public static string ToAdd = ConfigurationManager.AppSettings["emailAdd"];
        static int port = Int32.Parse(ConfigurationManager.AppSettings["emailPortaSMTP"]);

        //Retorno bool, se for com sucesso é true, caso contrário é false.
        public static void enviar(string assunto, string texto, string emailSending)
        {
            try
            {
                MailMessage mailMessage = new MailMessage();
                mailMessage.From = new MailAddress(from);

                mailMessage.To.Add(to);

                if(!String.IsNullOrEmpty(ToAdd))
                    mailMessage.To.Add(ToAdd);

                mailMessage.ReplyToList.Add(new MailAddress(emailSending));
                mailMessage.Subject = assunto;
                mailMessage.IsBodyHtml = true;
                mailMessage.Body = texto;

                SmtpClient smtpClient = new SmtpClient(smtp);
                //smtpClient.EnableSsl = true;
                smtpClient.Port = port;
                smtpClient.Credentials = new NetworkCredential(from, password);
                smtpClient.Send(mailMessage);

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}